/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.commons;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Juank
 */
public class WebMap extends HashMap {

    protected int initSize = 200;
    protected int initialShow = 2;
    protected int currentlyShowing = 2;
    private Class objectType;

    /**
     *
     * @param type
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public WebMap(Class type) throws InstantiationException, IllegalAccessException {
        objectType = type;
        initialize();
    }

    public WebMap(Class type, int initialShow) throws RuntimeException {
        try {
            objectType = type;
            setInitialShow(initialShow);
            setCurrentlyShowing(initialShow);
            initialize();
        } catch (Throwable thr) {
            throw new RuntimeException("Error de instanciacion :", thr);
        }
    }

    public void WebMapWithRuntimeException(Class type, int initialShow) throws RuntimeException {
        try {
            objectType = type;
            setInitialShow(initialShow);
            setCurrentlyShowing(initialShow);
            initialize();
        } catch (Throwable thr) {
            throw new RuntimeException("Error de instanciacion en el webMap", thr);
        }
    }

    public List getItemsToShow() {
        List forms = new ArrayList(0);
        for (int i = 0; i < getCurrentlyShowing(); i++) {
            forms.add(this.get(String.valueOf(i)));
        }
        return forms;
    }

    public Iterator iterator() {
        Collection temp = getItemsToShow();
        if (temp != null && !temp.isEmpty()) {
            return temp.iterator();
        } else {
            return (new Vector(0)).iterator();
        }
    }

    public Object getItem(Object key) throws Exception {
        if (this.containsKey(key)) {
            return this.get(key);
        } else {
            throw new Exception("Element with key-->" + key + " is not available");
        }
    }

    public void showOtherItem() {
        currentlyShowing = getCurrentlyShowing() + 1;
    }

    public void addElement(String key, Object obj) {
        this.put(key, obj);
        currentlyShowing = getCurrentlyShowing() + 1;
    }

    public void addElement(Object obj) {
        addElement(String.valueOf(getCurrentlyShowing()), obj);
    }

    public void removeElement(String key) {
        this.remove(key);
        currentlyShowing--;
    }

    private void initialize() throws IllegalAccessException, InstantiationException {
        for (int i = 0; i < initSize; i++) {
            Object obj = objectType.newInstance();
            put(String.valueOf(i), obj);
        }
    }

    public int getInitialShow() {
        return initialShow;
    }

    public int getInitSize() {
        return initSize;
    }

    public void setInitialShow(int initialShow) {
        this.initialShow = initialShow;
    }

    public void setInitSize(int initSize) {
        this.initSize = initSize;
    }

    public int getCurrentlyShowing() {
        if (currentlyShowing < initialShow) {
            return initialShow;
        }
        return currentlyShowing;
    }

    public void setCurrentlyShowing(int currentlyShowing) {
        this.currentlyShowing = currentlyShowing;
    }

    public Class getObjectType() {
        return objectType;
    }

    public void setObjectType(Class objectType) {
        this.objectType = objectType;
    }

    public String getCurrentlyShowingStr() {
        return String.valueOf(getCurrentlyShowing());
    }

    public void setCurrentlyShowingStr(String currentlyShowing) {
        this.currentlyShowing = Integer.parseInt(currentlyShowing);
    }

    public void clearAndAddAll(List list) throws IllegalAccessException, InstantiationException {
        initialize();
        currentlyShowing = 0;
        int i = 0;
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            addElement(String.valueOf(i), element);
            i++;
        }
    }
}
